//
//  CustomView.m
//  CAShapeLayer
//
//  Created by TengFeiLi on 15/7/14.
//  Copyright (c) 2015年 TengFeiLi. All rights reserved.
//

#import "CustomView.h"
#import "POP.h"
@interface CustomView (){
    CAShapeLayer *progressLayer;
}
@end

@implementation CustomView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self.layer setCornerRadius:20];
        [self.layer setBackgroundColor:[UIColor colorWithRed:0.16 green:0.72 blue:1.0 alpha:1.0].CGColor];
    }
    return self;
}

-(void)progress:(CGPoint)point{
    

}
-(void)loading{
    self.userInteractionEnabled = NO;
    [self startAnimate];
}

-(void)reset{
    
    [self.layer pop_removeAllAnimations];
    self.userInteractionEnabled = YES;
    
    for (CALayer * layer in self.layer.sublayers) {
        if([layer isKindOfClass:[CAShapeLayer class]]){
            [layer removeFromSuperlayer];
        }
    }
    
    self.layer.opacity = 1.0;
    self.layer.transform = CATransform3DIdentity;
    [self.layer setMasksToBounds:YES];
    [self.layer setBackgroundColor:[UIColor colorWithRed:0.16 green:0.72 blue:1.0 alpha:1.0].CGColor];
    [self setBounds:CGRectMake(0.0f, 0.0f, 100.0f, 50.0f)];
    [self.layer setCornerRadius:self.bounds.size.width / 5];
    [self setTitle:@"上传" forState:UIControlStateNormal];
}

-(void)startAnimate{
    
    [self pop_removeAllAnimations];
    
    //Config progress line
    progressLayer = [CAShapeLayer layer];
    progressLayer.strokeColor = [UIColor colorWithWhite:1.0 alpha:0.98].CGColor;
    progressLayer.lineCap   = kCALineCapRound;
    progressLayer.lineJoin  = kCALineJoinBevel;
    progressLayer.lineWidth = 8.0;
    progressLayer.strokeEnd = 0.0;
    progressLayer.fillColor = nil;
    
    NSLog(@"%@",NSStringFromCGPoint(self.center));
    
    UIBezierPath *progressline = [UIBezierPath bezierPathWithArcCenter:CGPointMake(50, 50)
                                                                radius:self.bounds.size.width / 2 - 10
                                                            startAngle: 0.0
                                                              endAngle:M_PI * 2
                                                             clockwise:YES];
    NSLog(@"%@",NSStringFromCGRect(self.bounds));
    
    progressLayer.path = progressline.CGPath;
    progressLayer.strokeColor = [UIColor whiteColor].CGColor;
    [self.layer addSublayer:progressLayer];
    

    POPSpringAnimation * boundsAnim = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerBounds];
    boundsAnim.springBounciness = 60;
    boundsAnim.springSpeed = 6;

    boundsAnim.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0,100, 100)];
    
    
    POPSpringAnimation * radiusAnim = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerCornerRadius];
    radiusAnim.springBounciness = 1;
    radiusAnim.springSpeed = 10;
    radiusAnim.toValue = [NSNumber numberWithFloat:self.bounds.size.width/2];
    
    radiusAnim.completionBlock =  ^(POPAnimation * anim,BOOL finished){
        if(finished){
            POPBasicAnimation * progressBoundsAnim = [POPBasicAnimation animationWithPropertyNamed:kPOPShapeLayerStrokeEnd];
            progressBoundsAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
            progressBoundsAnim.duration = 2.0;
            progressBoundsAnim.fromValue = @(0.0);
            progressBoundsAnim.toValue = @(1.0);
            
            [progressLayer pop_addAnimation:progressBoundsAnim forKey:@"AnimateBounds"];
            progressBoundsAnim.completionBlock = ^(POPAnimation *anim, BOOL finished) {
                if (finished) {
                    
                    UIBezierPath * path = [UIBezierPath bezierPath];
                    [path moveToPoint:CGPointMake(20, 55)];
                    [path addCurveToPoint:CGPointMake(80, 30) controlPoint1:CGPointMake(30, 100) controlPoint2:CGPointMake(65, 90)];
                    progressLayer.path = path.CGPath;
                    
                    
                    POPBasicAnimation * finish = [POPBasicAnimation animationWithPropertyNamed:kPOPShapeLayerStrokeEnd];
                    finish.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
                    finish.duration = 0.4;
                    finish.fromValue = @(0.0);
                    finish.toValue = @(1.0);
                    
                    [progressLayer pop_addAnimation:finish forKey:@"finish"];

                    
                    [self setTitle:@"完成" forState:UIControlStateNormal];
                }
            };
        }
    };
    
    [self.layer pop_addAnimation:radiusAnim forKey:@"radius"];
    [self.layer pop_addAnimation:boundsAnim forKey:@"bounds"];
    
    
}
-(void)finish{

}
@end
